/**
 * Created by Vicky on 2017/2/23.
 */
"use strict"

//引包
const path = require("path");
const xtpl = require("xtpl");
const fs = require("fs");

//引数据库中间件
const dataManager = require(path.join(__dirname, "../tools/dataManager.js"));

//暴露
module.exports = {
    //学生信息列表
    getListPage: (req, res)=> {
        const keyword = req.query.keyword || '';
        const username=req.session.username;
        dataManager.find("studentInfo", {name:{$regex:keyword}}, (studentList)=> {
            xtpl.renderFile(path.join(__dirname, "../views/studentlist.html"), {username:username,studentList: studentList}, (err, content)=> {
                if (err) {
                    console.log(err);
                    return;
                }

                res.setHeader("Content-Type", "text/html;charset=utf-8");
                res.end(content);
            })

        })
    },

    //增加页面
    getAddPage: (req, res)=> {
        const username=req.session.username;
        xtpl.renderFile(path.join(__dirname, "../views/add.html"), {username:username}, (err, content)=> {
            if (err) {
                console.log(err);
                return;
            }

            res.setHeader("Content-Type", "text/html;charset=utf-8");
            res.end(content);
        });
    },

    //保存增加的内容
    saveAddContent: (req, res)=> {
        //由于前面做了简单的验证，所以此时param是必须值
        const params = req.body;

        dataManager.insertOne("studentInfo", params, (isSuccess)=> {

            res.setHeader("Content-Type", "text/html;charset=utf-8");
            if (isSuccess) {
                //说明插入成功，跳转studentlist页面
                res.end("<script>window.location='/studentmanager/list';</script>")

            } else {
                //说明没有插入成功，调出框，停留在当前页面
                res.end("<script>alert('没有添加成功');</script>")

            }

        })

    },

    //编辑页面
    getEditPage:(req,res)=>{
        //获取路由传递过来的id
        const studentId=req.params.studentId;
        const username=req.session.username;
        dataManager.findOne("studentInfo",{_id:dataManager.ObjectId(studentId)},(studentInfo)=>{
            xtpl.renderFile(path.join(__dirname, "../views/edit.html"), {studentInfo:studentInfo,username:username}, (err, content)=>{
                if(err){
                    console.log(err);
                    return;
                }

                res.setHeader("Content-Type", "text/html;charset=utf-8");
                res.end(content);
            });
        })
    },

    //保存编辑内容
    saveEditContent:(req,res)=>{
        const params=req.body;
        const studentId=req.params.studentId;
        dataManager.updateOne("studentInfo",{_id:dataManager.ObjectId(studentId)},params,(isSuccess)=>{
            if(isSuccess){
                //保存成功,跳转到list页面
                res.end("<script>window.location='/studentmanager/list';</script>")
            }else{
                //保存成功,跳转到list页面
                res.end("<script>alert('保存失败')</script>")
            }
        });
    },

    //删除
    delete:(req,res)=>{
        const studentId=req.params.studentId;

        dataManager.deleteOne("studentInfo",{_id:dataManager.ObjectId(studentId)},(isSuccess)=>{
            let data={
                status:1,
                message:"删除成功"
            }
            if(!isSuccess){
                data.status=0;
                data.message="删除失败";
            }
            res.json(data);
        });

    }
}